package me.zhengjie.controller.merge;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.dto.MergeTaskQueryCriteria;
import me.zhengjie.server.MergeTaskService;
import me.zhengjie.utils.IpUtils;
import me.zhengjie.utils.PageResult;
import me.zhengjie.vo.merge.*;
import me.zhengjie.dao.MergeTaskDao;
import me.zhengjie.server.MaterialService;
import me.zhengjie.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mergeTask")
public class MergeTaskController {

    private static final Logger mergeTaskLog = LoggerFactory.getLogger("merge");

    @Resource
    private MergeTaskService mergeTaskService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('mergeTask:list')")
    public void exportMergeTask(HttpServletResponse response, MergeTaskQueryCriteria criteria) throws IOException {
        mergeTaskService.download(mergeTaskService.queryAll(criteria), response);
    }


    @PostMapping
    @Log("新增MergeTaskController")
    @ApiOperation("新增MergeTaskController")
    @PreAuthorize("@el.check('mergeTask:add')")
    public ResponseEntity<Object> createMergeTask(@Validated @RequestBody MergeTaskDao resources) {
        mergeTaskService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改MergeTaskController")
    @ApiOperation("修改MergeTaskController")
    @PreAuthorize("@el.check('mergeTask:edit')")
    public ResponseEntity<Object> updateMergeTask(@Validated @RequestBody MergeTaskDao resources) {
        mergeTaskService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除MergeTaskController")
    @ApiOperation("删除MergeTaskController")
    @PreAuthorize("@el.check('mergeTask:del')")
    public ResponseEntity<Object> deleteMergeTask(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        mergeTaskService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("findMergeStoreTask")
    public ResponseEntity<PageResult<MergeTaskDao>> findMergeStoreTask(MergeTaskQueryCriteria criteria) {
        mergeTaskLog.info("查询合箱/托任务清单");
        Page<MergeTaskDao> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(mergeTaskService.findMergeStoreTask(criteria, page), HttpStatus.OK);
    }

    @GetMapping("/findMergeMaterialInfo")
    public ResponseEntity<PageResult<MergeCreateResponseVo>> findMergeMaterialInfo(MergeRequestVo mergeRequestVo) {
        mergeTaskLog.info("查询可合箱/托物料查询对象");
        return new ResponseEntity<>(mergeTaskService.findMergeMaterialInfo(mergeRequestVo), HttpStatus.OK);
    }

    @RequestMapping(value = "/mergeCreateContainerInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> mergeCreateContainerInfo(@RequestBody MergeContainerRequestVo request, HttpServletRequest httpServletRequest) {
        mergeTaskLog.info("创建合箱/板出库任务单");
        String remoteIp = IpUtils.getRemoteIp(httpServletRequest);
        request.setWorkIP(remoteIp);
        return mergeTaskService.mergeCreateContainerInfo(request);
    }

    @RequestMapping(value = "/startMergeTaskInfo", method = RequestMethod.POST)
    public Result<?> startMergeTaskInfo(@RequestBody Map<String, String> params) {
        mergeTaskLog.info("开启合并任务");
        return mergeTaskService.startMergeTaskInfo(params);
    }

    /**
     * 定时任务处理出库
     */
    @Scheduled(fixedDelay = 5000)
    public void ScheduledFindStoreMergeTask() {
        mergeTaskService.ScheduledFindStoreMergeTask();
    }

    @RequestMapping(value = "/findMergeTaskInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<MergeContainerList>> findMergeTaskInfo(@RequestBody Map<String, Object> params) {
        mergeTaskLog.info("查询正在合箱载具物料信息");
        return mergeTaskService.findMergeTaskInfo(params);
    }

    @RequestMapping(value = "/againBindContainerInfo", method = RequestMethod.POST)
    public Result<?> againBindContainerInfo(@RequestBody AgainBindVo againBindVo) {
        mergeTaskLog.info("重新绑定载具编码");
        List<String> itemBarCodeList = againBindVo.getItemBarCodeList();
        String containerCode = againBindVo.getContainerCode();
        if (itemBarCodeList == null || containerCode == null) {
            return Result.error("参数不能为空");
        }
        return mergeTaskService.againBindContainerInfo(itemBarCodeList, containerCode);
    }

    @RequestMapping(value = "/successBindContainer", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> successBindContainer(@RequestBody Map<String, String> params) {
        mergeTaskLog.info("点击单个载具完成");
        // 更新状态
        return mergeTaskService.successBindContainer(params);
    }

    @RequestMapping(value = "/allSuccessBindContainer", method = RequestMethod.POST)
    public Result<?> allSuccessBindContainer(@RequestBody Map<String, String> params) {
        mergeTaskLog.info("点击任务单完成");
        if (params == null || params.get("taskId") == null) {
            return Result.error("参数不能为空");
        }
        String taskId = params.get("taskId");
        // 更新状态
        return mergeTaskService.allSuccessBindContainer(taskId);
    }

}
